package com.crayon.scoreboard.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.aphidmobile.flip.FlipViewController;
import com.crayon.scoreboard.util.GDB;
import com.crayon.scoreboard.R;
import com.crayon.scoreboard.views.NumberTextView;


/**
 * com.crayon.scoreboard.fragment.FlipTextViewFragment
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link FlipTextViewFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link FlipTextViewFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FlipTextViewFragment extends Fragment {

    public static final String PARAM_COLOR="colorbg";

    protected FlipViewController flipView;
    FlipViewController.ViewFlipListener flipListener;

    public static FlipTextViewFragment newInstance(int bgcolor) {
        FlipTextViewFragment fragment = new FlipTextViewFragment();
        Bundle args = new Bundle();
        args.putInt(PARAM_COLOR, bgcolor);
        fragment.setArguments(args);
        return fragment;
    }

    public interface FlipTextSelectedFragmentListener{
        public void OnFlipSelectedScoreAtFragment(FlipTextViewFragment fragment,View view, int position);
    }


    FlipTextSelectedFragmentListener selectedFragmentListener;

    public FlipTextSelectedFragmentListener getSelectedFragmentListener() {
        return selectedFragmentListener;
    }

    public void setSelectedFragmentListener(FlipTextSelectedFragmentListener selectedFragmentListener) {
        this.selectedFragmentListener = selectedFragmentListener;
    }

    public int getFlipBoardIndex(){
        if(flipView==null) return -0;
        return flipView.getSelectedItemPosition();
    }

    public void incressCount(){

        flipView.setSelection(flipView.getSelectedItemPosition()+1);
    }

    public void setSelectIndex(int position){

        flipView.setSelection(position);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        flipView = new FlipViewController(inflater.getContext());



        flipView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return 999;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                NumberTextView view;

                if (convertView == null) {
                    final Context context = parent.getContext();
                    view = new NumberTextView(context, position);
                    view.setTextSize(context.getResources().getDimension(R.dimen.textSize));
                    view.setBackgroundColor(getArguments().getInt(PARAM_COLOR));
                } else {
                    view = (NumberTextView) convertView;
                    view.setNumber(position);
                }

                return view;
            }
        });

        flipView.setOnViewFlipListener(viewFlipListener);
        return flipView;
    }

    private FlipViewController.ViewFlipListener viewFlipListener =new FlipViewController.ViewFlipListener() {
        @Override
        public void onViewFlipped(View view, int position) {
            GDB.log("..selectedFragmentListener vauel " + getSelectedFragmentListener());
            if(getSelectedFragmentListener()!=null){
                onCallBack(view,position);
            }
        }
    };
    private void onCallBack(View view, int position){
        if(getSelectedFragmentListener()!=null){
            getSelectedFragmentListener().OnFlipSelectedScoreAtFragment(this,view,position);
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        flipView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        flipView.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        flipView = null;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    public void setOnViewFlipListener(FlipViewController.ViewFlipListener flipListener){
        this.flipListener=flipListener;
        if(flipView==null)return;
        flipView.setOnViewFlipListener(flipListener);
    }
}
